home *** CD-ROM | disk | FTP | other *** search
/ Quick PC 61 / Quick PC 61.iso / I386 / CMDLIB.WS_ / cmdlib.wsc
Encoding:
Extensible Markup Language  |  2003-02-21  |  41.9 KB  |  1,052 lines

  1. <?xml version="1.0"?>
  2. <package>
  3. <?component error="true"?>
  4. <component>
  5. <registration
  6.    description="VBS Library"
  7.    progid="Microsoft.CmdLib"
  8.    version="1"
  9.    classid="{6D335ADF-8270-4805-A044-2B6A09476396}">
  10. </registration>
  11.  
  12. <public>
  13. <comment>
  14. ******************************************************************************
  15.      Copyright (c) Microsoft Corporation. All rights reserved. 
  16.     
  17.      Module Name:    CmdLib.wsc 
  18.     
  19.      Abstract:       This module contains the common functionality.
  20.     
  21. *******************************************************************************
  22. </comment>
  23.    <method name="checkScript"/>
  24.    <method name="vbPrintf"/>
  25.    <method name="getHostName"/>
  26.    <method name="getUserName"/>
  27.    <method name="getDomainName"/>
  28.    <method name="LengthinBytes"/>
  29.    <method name="LeftBytes"/>
  30.    <method name="getPassword"/>
  31.    <method name="trapError"/>
  32.    <method name="getArguments"/>
  33.    <method name="wmiConnect"/>
  34.    <method name="packString"/>
  35.    <method name="getMaxStringLen"/>
  36.    <method name="showResults"/>
  37.    <method name="validateDateTime"/>
  38.    <method name="changeToWMIDateTime"/>
  39.    <method name="matchPattern"/>
  40.    <property name="ScriptingHost" internalName="WScript"/>
  41. </public>
  42.  
  43. <resource id="PATTERN_VBPRINTF">%\d</resource>
  44. <resource id="L_INVALID_ERRORMESSAGE_TYPE_AS_INPUT">ERROR: Invalid type passed as input to the function.</resource>
  45. <resource id="L_INVALID_ERRORMESSAGE_ARG_NUMBER_AS_INPUT_ERRORMESSAGE">ERROR: Invalid number of arguments passed to the Print function.</resource>
  46. <resource id="TEXT_NA">N/A</resource>
  47. <resource id="OBJ_SYSTEMINFO_CLASS">Win32_ComputerSystem</resource>
  48. <resource id="L_INVALID_ERRORMESSAGE">ERROR: Invalid '%1'.</resource>
  49. <resource id="L_INVALID_SYNTAX_ERRORMESSAGE">ERROR: Invalid Syntax. Value expected for '%1'.</resource>
  50. <resource id="L_HELP_SYNTAX_MESSAGE">Type "%1 /?" for usage.</resource>
  51. <resource id="HINT_CHECK_INPUT">Please check the input and try again.</resource>
  52. <resource id="L_ERROR_CHECK_VBSCRIPT_VERSION_ERRORMESSAGE">Unexpected Error: Please check the current version of VBScript.</resource>
  53. <resource id="PATTERN_NEGATIVE_NUMBER">^\-\d|\d+$</resource>
  54. <resource id="CONST_NO_MATCHES_FOUND">0</resource>
  55. <resource id="OBJ_SCRIPTING_LOCATOR">WbemScripting.SWbemLocator</resource>
  56. <resource id="DISPLAY_FMT_TABLE_TEXT">TABLE</resource>
  57. <resource id="DISPLAY_FMT_CSV_TEXT">CSV</resource>
  58. <resource id="DISPLAY_FMT_LIST_TEXT">LIST</resource>
  59. <resource id="EXIT_SUCCESS">0</resource>
  60. <resource id="EXIT_INVALID_PARAM">999</resource>
  61. <resource id="EXIT_UNEXPECTED">255</resource>
  62. <resource id="EXIT_INVALID_INPUT">254</resource>
  63. <resource id="EXIT_METHOD_FAIL">250</resource>
  64. <resource id="L_INVALID_ERRORMESSAGE_TIME_ERRORMESSAGE">ERROR: Invalid time '%1' specified for the filter '%2'.</resource>
  65. <resource id="L_INVALID_ERRORMESSAGE_DATE_ERRORMESSAGE">ERROR: Invalid date '%1' specified for the filter '%2'.</resource>
  66. <resource id="L_ENTER_PASSWORD_TEXT">Enter the Password:</resource>
  67. <resource id="L_PROCESSING_TEXT">Processing...</resource>
  68. <resource id="OBJ_SCRIPT_PASSWORD">ScriptPW.Password.1</resource>
  69. <resource id="L_HINT_CHECK_PASSWORD_DLL_MESSAGE">HINT: Please check if ScriptPW.dll is registered in the system.</resource>
  70. <resource id="CONST_ERROR">0</resource>
  71. <resource id="CONST_CSCRIPT">2</resource>
  72. <resource id="L_WARRING_LOCAL_CREDENTIALS_SUPPLIED_MESSAGE">WARNING: Ignoring the user credentials for the local connection.</resource>
  73. <resource id="CONST_LOCAL_CREDENTIALS_SUPPLIED">-2147217308</resource>
  74.  
  75. <script language="VBScript">
  76.     <![CDATA[
  77.  
  78.         ' All the functions which are used in common across all the vbs scripts are defined below
  79.  
  80.         ' Function used to find whether CScript is used or not
  81.         '********************************************************************
  82.         '* Function: checkScript
  83.         '*
  84.         '* Purpose:  Determines which program is used to run this script.
  85.         '*
  86.         '* Input:    None
  87.         '*
  88.         '* Output:   intChkProgram is set to one of CONST_ERROR or CONST_CSCRIPT.
  89.         '*
  90.         '********************************************************************
  91.         Function checkScript()
  92.             ON ERROR RESUME NEXT
  93.             Err.Clear
  94.  
  95.             Dim strFullName    'program with its full path - used to execute the script 
  96.             Dim strCommand    'name of program without extension (like exe, Eg:CScript)
  97.             Dim intExe_Index    'to calculate the position of .exe in strFullName 
  98.             Dim intSlash_Index    'to calculate the position of \ (slash) in strFullName
  99.  
  100.             'strFullName should be something like C:\WINDOWS\COMMAND\CSCRIPT.EXE
  101.             strFullName = WScript.FullName
  102.  
  103.             If Err.Number then
  104.                 Wscript.Echo "Error 0x" & CStr(Hex(Err.Number)) 
  105.                 If Err.Description <> "" Then
  106.                     Wscript.Echo "Error description: " & Err.Description & "."
  107.                 End If
  108.                 Err.Clear
  109.                 checkScript =   getResource("CONST_ERROR")
  110.                 Exit Function
  111.             End If
  112.  
  113.             intExe_Index = InStr(1, strFullName, ".exe", 1)
  114.  
  115.             If intExe_Index = 0 Then
  116.                 checkScript = getResource("CONST_ERROR")
  117.                 Exit Function
  118.             Else
  119.                 intSlash_Index = InStrRev(strFullName, "\", intExe_Index, 1)
  120.  
  121.                 If intSlash_Index = 0 Then
  122.                     checkScript =  getResource("CONST_ERROR")
  123.                     Exit Function
  124.                 Else
  125.                     strCommand = Mid(strFullName, intSlash_Index+1, _
  126.                                     intExe_Index-intSlash_Index-1)
  127.  
  128.                     If LCase(strCommand) = LCase("cscript") Then 
  129.                         checkScript =  getResource("CONST_CSCRIPT")
  130.                     Else
  131.                         checkScript = getResource("CONST_ERROR")
  132.                     End If
  133.  
  134.                 End If  'If intSlash_Index = 0 Then
  135.  
  136.             End If      'If intExe_Index = 0 Then
  137.  
  138.         End Function
  139.  
  140.         ' Subroutine which implements normal printf functionality
  141.         '********************************************************************
  142.         '* Sub:     vbPrintf
  143.         '*
  144.         '* Purpose: Simulates the Printf function.
  145.         '*
  146.         '* Input:  [in]  strPhrase      the string with '%1 %2 &3 ' in it
  147.         '*         [in]  args           the values to replace '%1 %2 ..etc' with
  148.         '*
  149.         '* Output:  Displays the string on the screen
  150.         '*          (All the '%x' variables in strPhrase is replaced by the 
  151.         '*           corresponding elements in the array)
  152.         '*
  153.         '********************************************************************
  154.         Sub vbPrintf(ByVal strPhrase, ByVal args )
  155.  
  156.             ON ERROR RESUME NEXT
  157.             Err.Clear
  158.  
  159.             'Changed for localization  
  160.  
  161.             Dim strMatchPattern         ' the pattern to match - '%[number]'
  162.             Dim intValuesCount          ' to get the count of matching results
  163.             Dim i                       ' used in the loop
  164.             Dim strTemp                 ' to store temporally  the given input string  for formatting
  165.  
  166.             strTemp   = strPhrase
  167.  
  168.             ' look out for '%[number]' in the given string
  169.             strMatchPattern = getResource("PATTERN_VBPRINTF") '"\%[number]"
  170.  
  171.             intValuesCount = matchPattern (strMatchPattern, strTemp)
  172.  
  173.             If intValuesCount <> 0 Then
  174.                 ' if present then replace '%1 %2 %3' in the string by
  175.                 ' corresponding element in the given array
  176.  
  177.                 If Not IsArray(args) Then
  178.                     WScript.Echo getResource("L_INVALID_ERRORMESSAGE_TYPE_AS_INPUT")
  179.                     WScript.Quit getResource("EXIT_INVALID_PARAM")
  180.                 End If
  181.                
  182.                 If intValuesCount <> UBound(args)+1 Then
  183.                     WScript.Echo getResource("L_INVALID_ERRORMESSAGE_ARG_NUMBER_AS_INPUT_ERRORMESSAGE")
  184.                     WScript.Quit getResource("EXIT_INVALID_PARAM")
  185.                 End If
  186.  
  187.                For i = 1 to intValuesCount
  188.                     strPhrase = Replace(strPhrase, "%" & Cstr(i), (args(i-1) ), 1, 1, VBBinaryCompare)
  189.                 Next
  190.  
  191.             End If
  192.  
  193.            WScript.Echo(strPhrase)
  194.  
  195.         End Sub
  196.  
  197.         ' Function which checks whether a given value matches a particular pattern
  198.         '********************************************************************
  199.         '* Function: matchPattern
  200.         '*
  201.         '* Purpose:  To check if the given pattern is existing in the string
  202.         '*
  203.         '* Input:
  204.         '*  [in]     strMatchPattern   the pattern to look out for
  205.         '*  [in]     strPhrase         string in which the pattern needs to be checked
  206.         '*
  207.         '* Output:   Returns number of occurrences if pattern present, 
  208.         '*           Else returns CONST_NO_MATCHES_FOUND
  209.         '*
  210.         '********************************************************************
  211.         Function matchPattern(ByVal strMatchPattern, ByVal strPhrase)
  212.  
  213.             ON ERROR RESUME NEXT
  214.             Err.Clear
  215.  
  216.             Dim objRegEx        ' the regular expression object
  217.             Dim Matches         ' the results that match the given pattern
  218.             Dim intResultsCount ' the count of Matches
  219.             
  220.             intResultsCount = 0  ' initialize the count to 0
  221.  
  222.             'create instance of RegExp object
  223.             Set objRegEx = New RegExp 
  224.             If (NOT IsObject(objRegEx)) Then
  225.                 WScript.Echo (getResource("L_ERROR_CHECK_VBSCRIPT_VERSION_ERRORMESSAGE"))
  226.             End If
  227.             'find all matches
  228.             objRegEx.Global = True
  229.             'set case insensitive
  230.             objRegEx.IgnoreCase = True
  231.             'set the pattern
  232.             objRegEx.Pattern = strMatchPattern
  233.  
  234.             Set Matches = objRegEx.Execute(strPhrase)
  235.             intResultsCount = Matches.Count
  236.  
  237.             'test for match
  238.             If intResultsCount > 0 Then
  239.                 matchPattern = intResultsCount
  240.             Else
  241.                 matchPattern = getResource("CONST_NO_MATCHES_FOUND")
  242.             End If
  243.  
  244.         End Function
  245.  
  246.         ' Function used to get the current Host name
  247.         '********************************************************************
  248.         '* Function: getHostName
  249.         '*
  250.         '* Purpose:  To get the Host Name
  251.         '*
  252.         '* Input:   objService                        ' the service object
  253.         '*
  254.         '* Output:   Returns the Host Name
  255.         '*
  256.         '********************************************************************
  257.         Function getHostName ( ByVal ObjService)
  258.             ON ERROR RESUME NEXT
  259.             Err.Clear
  260.  
  261.             Dim objSystemSet          ' to store the InstancesOf Class
  262.             Dim  System               ' to refer to the instances objSystemSet
  263.  
  264.            Set objSystemSet = objService.InstancesOf(getResource("OBJ_SYSTEMINFO_CLASS"))
  265.  
  266.             If Err.Number Then
  267.                 getHostName = getResource("TEXT_NA")
  268.                 Err.clear
  269.             Else
  270.                 For each System in objSystemSet
  271.                         If IsEmpty(System.Name) Then
  272.                              getHostName = getResource("TEXT_NA")
  273.                         Else
  274.                             getHostName = System.Name
  275.                         End If
  276.                         Exit for
  277.                 Next
  278.              End If
  279.           End Function
  280.  
  281.         ' Function used to get the current User Name
  282.         '********************************************************************
  283.         '* Function: getUserName
  284.         '*
  285.         '* Purpose:  To get the User Name
  286.         '*
  287.         '* Input:   objService                        ' the service object
  288.         '*
  289.         '* Output:   Returns the User Name
  290.         '*
  291.         '********************************************************************
  292.         Function getUserName ( ByVal ObjService)
  293.             ON ERROR RESUME NEXT
  294.             Err.Clear
  295.  
  296.             Dim objSystemSet          ' to store the InstancesOf Class
  297.             Dim  System               ' to refer to the instances objSystemSet
  298.  
  299.             Set objSystemSet = objService.InstancesOf(getResource("OBJ_SYSTEMINFO_CLASS"))
  300.  
  301.             If Err.Number Then
  302.                 getUserName = getResource("TEXT_NA")
  303.                 Err.clear
  304.             Else
  305.                 For each System in objSystemSet
  306.                         If IsEmpty(System.UserName) Then
  307.                              getUserName = getResource("TEXT_NA")
  308.                         Else
  309.                              getUserName = System.UserName
  310.                         End If
  311.                         Exit for
  312.                 Next
  313.             End If
  314.         End Function
  315.  
  316.         ' Function used to get the current Domain name
  317.         '********************************************************************
  318.         '* Function: getDomainName
  319.         '*
  320.         '* Purpose:  To get the Domain Name
  321.         '*
  322.         '* Input:  objService                        ' the service object
  323.         '*
  324.         '* Output:   Returns the Domain Name
  325.         '*
  326.         '********************************************************************
  327.         Function getDomainName( ByVal ObjService)
  328.             ON ERROR RESUME NEXT
  329.             Err.Clear
  330.  
  331.             Dim objSystemSet          ' to store the InstancesOf Class
  332.             Dim  System               ' to refer to the instances objSystemSet
  333.  
  334.             Set objSystemSet = objService.InstancesOf(getResource("OBJ_SYSTEMINFO_CLASS"))
  335.  
  336.             If Err.Number Then
  337.                 getDomainName = getResource("TEXT_NA")
  338.                 Err.clear
  339.             Else
  340.                For each System in objSystemSet
  341.                         If IsEmpty(System.Domain) Then
  342.                              getDomainName = getResource("TEXT_NA")
  343.                         Else
  344.                              getDomainName = System.Domain
  345.                         End If
  346.                         Exit for
  347.                 Next
  348.             End If
  349.         End Function
  350.  
  351.         ' Function used to get the password from the user
  352.         '**********************************************************************
  353.         '* Function: getPassword
  354.         '*
  355.         '* Purpose:  To get password from the user
  356.         '*
  357.         '* Input:    None
  358.         '*
  359.         '* Output:   Returns the Password specified by the user
  360.         '*
  361.         '**********************************************************************
  362.         Function getPassword()
  363.             ON ERROR RESUME NEXT
  364.             Err.Clear
  365.  
  366.             Dim objPassword     ' the object to store  password.dll 
  367.  
  368.             WScript.Echo getResource("L_ENTER_PASSWORD_TEXT")
  369.             Set objPassword = CreateObject(getResource("OBJ_SCRIPT_PASSWORD"))
  370.             If NOT IsObject(objPassword) Then
  371.                  ' error in getting the password
  372.                 WScript.Echo("")         'blank line
  373.                 WScript.Echo(getResource("L_HINT_CHECK_PASSWORD_DLL_MESSAGE"))
  374.                 WScript.Quit(getResource("EXIT_UNEXPECTED"))
  375.             End If
  376.  
  377.             getPassword = objPassword.GetPassword
  378.             ' WScript.Echo getResource("L_PROCESSING_TEXT")
  379.  
  380.         End Function
  381.  
  382.          ' Function used to trap error
  383.         '**********************************************************************
  384.         '* Function: trapError
  385.         '*
  386.         '* Purpose:  Reports error with a string saying what the error occurred in.
  387.         '*
  388.         '* Input:
  389.         '*   [in]    strIn        string saying what the error occurred in.
  390.         '*
  391.         '* Output:   displayed on screen
  392.         '*
  393.         '**********************************************************************
  394.         Function trapError (ByVal strIn)
  395.          ON ERROR RESUME NEXT    
  396.  
  397.             If Err.Number Then
  398.                 Wscript.Echo( "Error (0x" & CStr(Hex(Err.Number)) & "): " & strIn)
  399.                 If Err.Description <> "" Then
  400.                     Wscript.Echo( "Error description: " & Err.Description)
  401.                 End If
  402.                 Err.Clear
  403.                 trapError = TRUE
  404.             Else
  405.                 trapError = FALSE
  406.             End If
  407.         End Function
  408.  
  409.     ' Function used to get the arguments into appropriate variables
  410.         '**********************************************************************
  411.         '* Function: getArguments
  412.         '*
  413.         '* Purpose:  Gets the arguments specified into appropriate variables
  414.         '*
  415.         '* Input:
  416.         '*   [in]    StrVarName                stores the parameter
  417.         '*   [in]    strVar                    stores the parameter value
  418.         '*   [in]    intArgIter                counts the no.of arguments
  419.         '*   [in]    blnAllowNegativeValues    checks if negative parameter values are valid
  420.         '*
  421.         '* Output:   Returns TRUE or FALSE
  422.         '*
  423.         '**********************************************************************
  424.  
  425.         ' Function used to get the arguments into appropriate variables
  426.         Function getArguments ( ByVal StrVarName,   _
  427.                              ByRef strVar,       _
  428.                              ByRef intArgIter,   _
  429.                              ByVal blnAllowNegativeValues ) 
  430.             ON ERROR RESUME NEXT
  431.             Err.Clear
  432.  
  433.             'initialized to failure, changed to True upon successful completion
  434.             getArguments = False 
  435.  
  436.             intArgIter = intArgIter + 1
  437.  
  438.             If intArgIter > (Wscript.Arguments.Count - 1) Then
  439.                 vbPrintf getResource("L_INVALID_SYNTAX_ERRORMESSAGE"), Array(Wscript.Arguments.Item(intArgIter-1))
  440.                 Exit Function
  441.             End If
  442.  
  443.             strVar = Wscript.Arguments.Item(intArgIter)
  444.  
  445.             If Err.Number Then
  446.                 vbPrintf getResource("L_INVALID_ERRORMESSAGE"), Array(StrVarName)
  447.                 Call Wscript.Echo ( getResource("HINT_CHECK_INPUT") )
  448.                 Err.Clear
  449.                 Exit Function
  450.             End If
  451.  
  452.                 ' check for the input of   those  accept  negitive numeric values also.
  453.                 If blnAllowNegativeValues =True Then
  454.                         ' the input can be a negative number
  455.                         If matchPattern(getResource("PATTERN_NEGATIVE_NUMBER"), strVar) = getResource("CONST_NO_MATCHES_FOUND") Then
  456.                                 vbPrintf getResource("L_INVALID_ERRORMESSAGE"), Array(StrVarName)
  457.                                 Wscript.Echo ( getResource("HINT_CHECK_INPUT") )
  458.                                 Exit Function
  459.                         End If
  460.                 End If
  461.  
  462.              getArguments = True 'success
  463.  
  464.     End Function
  465.  
  466.     ' Function used to connect to wmi provider with the given credentials
  467.     '**************************************************************************
  468.     '* Function: wmiConnect
  469.     '*
  470.     '* Purpose:  Connects to machine strServer.
  471.     '*
  472.     '* Input:
  473.     '*   [in]    strServer       a machine name
  474.     '*   [in]    strNameSpace    a namespace
  475.     '*   [in]    strUserName     name of the current user
  476.     '*   [in]    strPassword     password of the current user
  477.     '*   [in/out] blnLocalConnection  a flag  for localConnection    
  478.     '*   [out]   objService      a service object
  479.     '*
  480.     '* Output:   objService is returned  as a service object.
  481.     '*
  482.     '**************************************************************************
  483.     Function wmiConnect( ByVal strNameSpace, _
  484.                          ByVal strUserName,  _
  485.                          ByVal strPassword,  _
  486.                          ByVal strServer,    _
  487.                          ByRef blnLocalConnection,   _
  488.                          ByRef objService    )
  489.  
  490.         ON ERROR RESUME NEXT
  491.         Err.Clear
  492.         Dim objLocator ' the locator object
  493.  
  494.         wmiConnect = True     ' There is no error.
  495.  
  496.         'Create Locator object to connect to remote CIM object manager
  497.         Set objLocator = CreateObject(getResource("OBJ_SCRIPTING_LOCATOR"))
  498.  
  499.         If Err.Number Then
  500.             wmiConnect = False     ' An error occurred
  501.             Exit Function
  502.         End If
  503.  
  504.         'Connect to the namespace which is either local or remote
  505.         Set objService = objLocator.ConnectServer (strServer, strNameSpace, _
  506.             strUserName, strPassword)
  507.  
  508.          If Err.Number <> 0 Then
  509.                 If Err.Number = Clng(getResource("CONST_LOCAL_CREDENTIALS_SUPPLIED")) Then
  510.  
  511.                         If  Not  blnLocalConnection =True  then
  512.                                 ' -2147217308 number to catch local credentails supplied by WMI
  513.                                 Wscript.echo getResource("L_WARRING_LOCAL_CREDENTIALS_SUPPLIED_MESSAGE")
  514.  
  515.                                 'setting the flag that target is local system to eleminate error message next time
  516.                                 blnLocalConnection = True
  517.                         End If 
  518.                         Err.Clear  ' clear the error number for local connection
  519.  
  520.                         ' Calling the Locator object to connect to local system
  521.                         Set objService = objLocator.ConnectServer(strServer, strNameSpace, "" , "" )
  522.                         If Err.Number <> 0 Then wmiConnect = False     ' An error occurred
  523.                   Else
  524.                         wmiConnect = False     ' An error occurred
  525.                   End If
  526.         End If
  527.  
  528.         ObjService.Security_.impersonationlevel = 3
  529.  
  530.     End Function
  531.  
  532.     ' Function used to pack the string to the given width
  533.     '**************************************************************************
  534.     '* Function: PackString
  535.     '*
  536.     '* Purpose:  Attaches spaces to a string to increase the length to intWidth.
  537.     '*
  538.     '* Input:
  539.     '*  [in]     strString    a string
  540.     '*  [in]     intWidth     the intended length of the string
  541.     '*
  542.     '* Output:   strPackString is returned as the packed (padded/truncated) string.
  543.     '*
  544.     '**************************************************************************
  545.     Function packString( ByVal strString, ByVal intWidth)
  546.         ON ERROR RESUME NEXT
  547.         Err.Clear
  548.  
  549.         strString = CStr(strString)
  550.         If Err.Number Then
  551.             Call Wscript.Echo (getResource("L_INVALID_ERRORMESSAGE_TYPE_AS_INPUT"))
  552.             Err.Clear
  553.             Wscript.Quit(getResource("EXIT_INVALID_PARAM"))
  554.         End If
  555.  
  556.         intWidth      = CInt(intWidth)
  557.  
  558.         If Err.Number Then
  559.             Call Wscript.Echo (getResource("L_INVALID_ERRORMESSAGE_TYPE_AS_INPUT"))
  560.             Err.Clear
  561.             Wscript.Quit(getResource("EXIT_INVALID_PARAM"))
  562.         End If
  563.  
  564.         If IsNull(strString) OR IsEmpty(strString) OR Len(strString) = 0 Then
  565.  
  566.             If intwidth = 0 then 
  567.  
  568.             packString = getResource("TEXT_NA") 
  569.             Exit Function
  570.  
  571.             Else
  572.  
  573.             packString = getResource("TEXT_NA") & Space(intWidth-3)
  574.             Exit Function
  575.  
  576.             End If   
  577.         End If
  578.         
  579.         If intWidth >= LengthinBytes(strString) Then
  580.         
  581.             packString = strString  & Space(intWidth-LengthinBytes(strString))
  582.  
  583.         Else
  584.  
  585.             ' Handling Output format independent to column width  (width=0)   
  586.         ' i.e print as it is . 
  587.             If intWidth = 0 then
  588.             ' print as it is.
  589.                 packString = LeftBytes(strString, LengthinBytes(strString))
  590.             Else
  591.                 ' truncate the string
  592.                 packString = LeftBytes(strString, intWidth)
  593.             End If
  594.  
  595.         End If
  596.  
  597.     End Function
  598.  
  599.     ' Function used to get length of the maximum length string in an array of strings
  600.     '**************************************************************************
  601.     '* Function: getMaxStringLength
  602.     '*
  603.     '* Purpose:  To get the length of longest string in the given array
  604.     '*
  605.     '* Input:    [in] arrStrings    an array of strings
  606.     '*
  607.     '* Output:   Returns length of longest string in the array
  608.     '*           If error in input, displays message and quits
  609.     '*
  610.     '**************************************************************************
  611.  
  612.     Function getMaxStringLen(ByVal arrStrings)
  613.         ON ERROR RESUME NEXT
  614.         Err.Clear
  615.  
  616.         Dim intMaxLength   ' to store the maximum length of the string
  617.         Dim intArrCount    ' used in the loop
  618.  
  619.         intMaxLength = 0
  620.         ' quit if input is not an array
  621.         If NOT IsArray(arrStrings) Then
  622.             WScript.Echo getResource("L_INVALID_ERRORMESSAGE_TYPE_AS_INPUT")
  623.             WScript.Quit(getResource("EXIT_INVALID_PARAM"))
  624.         End If
  625.  
  626.         ' check for length of each element in the array
  627.         For intArrCount = 0 To UBound(arrStrings)
  628.             If LengthinBytes(arrStrings(intArrCount)) > intMaxLength Then
  629.                 intMaxLength = LengthinBytes(arrStrings(intArrCount))
  630.             End If
  631.         Next
  632.         getMaxStringLen = intMaxLength
  633.     End Function
  634.  
  635.     ' Function used to get length of actual bytes required by the string.
  636.     '**************************************************************************
  637.     '* Function: LengthinBytes
  638.     '*
  639.     '* Purpose:  To get the length of a string in Bytes.
  640.     '*
  641.     '* Input:    [in] strString    a String
  642.     '*
  643.     '* Output:   Returns length of a string in Bytes.
  644.     '*
  645.     '**************************************************************************
  646.  
  647.     Function LengthinBytes(ByVal strString)
  648.     Dim i, strChar
  649.     LengthinBytes = 0
  650.     For i =1 To Len(strString)
  651.         strChar = Mid(strString, i, 1)
  652.         If Asc(strChar) > 255 OR Asc(strChar) < 0 Then
  653.         LengthinBytes = LengthinBytes + 2
  654.         Else
  655.             LengthinBytes = LengthinBytes + 1
  656.         End If
  657.     Next
  658.     End Function
  659.  
  660.     ' Function used to get left n number of bytes from a string.
  661.     '**************************************************************************
  662.     '* Function: LeftBytes
  663.     '*
  664.     '* Purpose:  To get left n number of bytes from a string.
  665.     '*
  666.     '* Input:    [in] strString    a String
  667.     '*
  668.     '* Output:   Returns a string containing n number of bytes.
  669.     '*
  670.     '**************************************************************************
  671.  
  672.     Function LeftBytes(ByVal strString, ByVal intBytesLength)
  673.     Dim i, strChar, LengthinBytes
  674.  
  675.     LengthinBytes = 0
  676.     LeftBytes = ""
  677.  
  678.     For i =1 To Len(strString)
  679.         strChar = Mid(strString, i, 1)
  680.         If AscW(strChar) > 255 OR AscW(strChar) < 0 Then
  681.         LengthinBytes = LengthinBytes + 2
  682.             Else
  683.             LengthinBytes = LengthinBytes + 1
  684.         End If
  685.  
  686.         'There will be a problem if intBytesLength is odd and LengthinBytes is even OR
  687.         'if intBytesLength is even and LengthinBytes is odd and the last character takes 2 bytes.
  688.  
  689.             If LengthinBytes = intBytesLength Then
  690.         LeftBytes = LeftBytes & strChar
  691.         Exit Function
  692.         ElseIf LengthinBytes > intBytesLength Then
  693.         LeftBytes = LeftBytes & Space(1)
  694.         Exit Function
  695.         Else
  696.         LeftBytes = LeftBytes & strChar
  697.         End If
  698.     Next
  699.  
  700.     End Function
  701.  
  702.  
  703.   ' Function used to show results in the desired format
  704.     '**************************************************************************
  705.     '* Function: showResults
  706.     '*
  707.     '* Purpose:  To show results in the desired format
  708.     '*
  709.     '* Input:   
  710.     '*        [in] arrHeader        an array of strings containing all the headers
  711.     '*        [in] arrResultsArray  array containing all the records
  712.     '*        [in] strFormat        CSV or LIST or TABLE
  713.     '*        [in] blnPrintHeader   Boolean value indicating whether header
  714.     '*                              should be printed or not
  715.     '*        [in] arrBlnHide       an array containing boolean values. Each value
  716.     '*                              indicates whether a particular value in a record
  717.     '*                              is to be displayed or not
  718.     '*
  719.     '* Output:   Displays all the records in the required format
  720.     '*
  721.     '**************************************************************************
  722.     Sub showResults( ByVal arrHeader,       _
  723.                      ByVal arrResultsArray, _
  724.                      ByVal arrMaxLength,    _
  725.                      ByVal strFormat,       _
  726.                      ByVal blnPrintHeader,  _
  727.                      ByVal arrBlnHide       )
  728.  
  729.         Dim i, j                   ' used as loop variables
  730.         Dim intTestResult          ' to store temporary results
  731.         Dim intMaxHeaderLength     ' to store length of longest column header
  732.         Dim strPackedString        ' to store the padded/truncated string
  733.         Dim arrResults             ' to store the row to display(which is an array)
  734.         Dim intColumnCount         ' to store the count for no.of columns
  735.  
  736.         ' get the maximum length of all the header names given
  737.         intMaxHeaderLength = getMaxStringLen(arrHeader)
  738.  
  739.         ' initialize the values
  740.         intColumnCount = UBound(arrHeader)
  741.         intTestResult  = 0
  742.  
  743.         Select Case LCase(strFormat)
  744.  
  745.             Case LCase(getResource("DISPLAY_FMT_LIST_TEXT"))
  746.                ' If LIST format is specified
  747.                For i = 0 to UBound(arrResultsArray)
  748.                     arrResults = arrResultsArray(i)
  749.                     For j =  0 to UBound(arrResults)
  750.                         If arrBlnHide(j) = 0 Then
  751.                             intTestResult = arrHeader(j) & ":"
  752.                             strPackedString = packString(intTestResult, intMaxHeaderLength+1)
  753.                             WScript.Echo strPackedString & " " & arrResults(j)
  754.                         End If
  755.                     Next
  756.                     ' print an empty line
  757.                     WScript.Echo ""
  758.              Next
  759.  
  760.             Case LCase(getResource("DISPLAY_FMT_CSV_TEXT"))
  761.                 ' If CSV format is specified
  762.                 If blnPrintHeader Then
  763.                         strPackedString = ""
  764.                         ' first print the header , if not already printed
  765.                         For i = 0 to UBound(arrHeader)
  766.                             If arrBlnHide(i) = 0 Then
  767.                                 intTestResult = InStr(1,arrHeader(i), ",", VBBinaryCompare)
  768.                                 If intTestResult > 0 Then
  769.                                     arrHeader(i) = chr(34) & arrHeader(i) & chr(34)
  770.                                 Else 
  771.                                   arrHeader(i) = chr(34) & arrHeader(i) & chr(34)
  772.                                 End If
  773.  
  774.                                 strPackedString = strPackedString & arrHeader(i)
  775.  
  776.                                 If (i+1) <= intColumnCount Then
  777.                                     strPackedString = strPackedString & ","
  778.                                 End If
  779.                             End If
  780.                         Next
  781.                         WScript.Echo strPackedString
  782.                 End If
  783.  
  784.                 ' print all the comma separated values
  785.                 For i = 0 to UBound(arrResultsArray)
  786.                     arrResults = arrResultsArray(i)
  787.                     strPackedString = ""
  788.                     For j =  0 to UBound(arrResults)
  789.                        If arrBlnHide(j) = 0 Then
  790.                         intTestResult = InStr(1,arrResults(j), ",", VBBinaryCompare)
  791.  
  792.                         If intTestResult > 0 Then
  793.                             strPackedString = strPackedString & chr(34) & arrResults(j) & chr(34)
  794.                         Else
  795.                             strPackedString = strPackedString & chr(34) & arrResults(j) & chr(34) 
  796.                         End If
  797.                         
  798.                         If (j+1) <= intColumnCount Then
  799.                             strPackedString = strPackedString & ","
  800.                           '  strPackedString = strPackedString & chr(34) & "," & chr(34)
  801.                         End If
  802.                     End If
  803.                     Next
  804.                     WScript.Echo strPackedString
  805.                     strPackedString = ""
  806.                 Next
  807.  
  808.             Case LCase(getResource("DISPLAY_FMT_TABLE_TEXT"))
  809.                 ' If table format is asked for
  810.                 If blnPrintHeader Then
  811.                     strPackedString = ""
  812.                     ' print the header, if not already printed
  813.                     For i = 0 to UBound(arrHeader)
  814.                     If arrBlnHide(i) = 0 Then
  815.                         strPackedString = strPackedString & " " & _
  816.                                             packString(arrHeader(i), _
  817.                                             arrMaxLength(i))
  818.                     End If
  819.                     Next
  820.  
  821.                     WScript.Echo strPackedString
  822.                     strPackedString = ""
  823.                     ' print the Underline to the column header
  824.                     For i =  0 to UBound(arrHeader)
  825.                        If arrBlnHide(i) = 0 Then
  826.  
  827.                        ' Handling Output format independent to column width  (width=0)   
  828.                        Dim FinalString  
  829.                        If arrMaxLength(i) = 0 then 
  830.                             FinalString  =    packString(String(LengthinBytes(arrheader(i)),"-"), arrMaxLength(i))
  831.                         Else
  832.                             FinalString  =     packString(String(arrMaxLength(i),"-"), arrMaxLength(i))
  833.                         End If
  834.                             strPackedString = strPackedString & " " & FinalString  
  835.                         End If
  836.                     Next    
  837.  
  838.                     WScript.Echo strPackedString
  839.                 End If
  840.  
  841.                 For i = 0 to UBound(arrResultsArray)
  842.                     arrResults = arrResultsArray(i)
  843.                     strPackedString = ""
  844.                     For j = 0 to UBound(arrResults)
  845.                     If arrBlnHide(j) = 0 Then
  846.                         strPackedString = strPackedString & " " & _
  847.                                        packString(arrResults(j), _
  848.                                        arrMaxLength(j))
  849.                      End If
  850.                     Next
  851.                     WScript.Echo strPackedString
  852.                 Next
  853.         End Select
  854.  
  855.     End Sub
  856.  
  857.     
  858.     '********************************************************************
  859.     '* Function: strDateTime
  860.     '*
  861.     '* Purpose:  To validate the date-time format specified
  862.     '*
  863.     '* Input:
  864.     '*           [in] strDateTime     the date-time string
  865.     '*
  866.     '* Output:   Returns true if valid format
  867.     '*           Else displays error message and quits
  868.     '*
  869.     '********************************************************************
  870.      Function validateDateTime(ByVal strDateTime)
  871.         ON ERROR RESUME NEXT
  872.         Err.Clear
  873.  
  874.         validateDateTime = False
  875.  
  876.         Dim arrDateTimeCheck    ' to store the date and time values
  877.         Dim intMonth            ' to store the month(instead of array(subscript))
  878.         Dim intDay              ' to store the day(instead of array(subscript))
  879.         Dim intYear             ' to store the year(instead of array(subscript))
  880.         Dim strTemp             ' to store temporary values
  881.         Dim arrTemp             ' to store temporary values when split is used
  882.         Dim intHour             ' to store the Hour(instead of array(subscript))
  883.         Dim intMinute           ' to store the Minutes(instead of array(subscript))
  884.         Dim intSecond           ' to store the Seconds(instead of array(subscript))
  885.  
  886.         ' strDateTime is of the format "mm/dd/yy|yyyy,hh:mm:ssPM"
  887.         ' first split at the comma and separate date and time
  888.         arrDateTimeCheck = split(strDateTime, ",",2,VBBinaryCompare)
  889.  
  890.         ' split the date and check if the month and day are in bounds
  891.         arrTemp = split(arrDateTimeCheck(0), "/",3,VBBinaryCompare)
  892.  
  893.         intMonth = arrTemp(0)
  894.         intDay   = arrTemp(1)
  895.         intYear  = arrTemp(2)
  896.  
  897.         If ((CInt(intMonth) < 1) OR (CInt(intMonth) > 12) OR (CInt(intDay) < 1) OR (CInt(intDay) > 31)) Then
  898.             vbPrintf getResource("L_INVALID_ERRORMESSAGE_DATE_ERRORMESSAGE"), Array(arrDateTimeCheck(0), strDateTime)
  899.             WScript.quit(getResource("EXIT_INVALID_INPUT"))
  900.             Exit Function
  901.         End If
  902.  
  903.         If CInt(year(arrDateTimeCheck(0))) => 9999 OR CInt(year(arrDateTimeCheck(0))) < 1601 then
  904.                 vbPrintf getResource("L_INVALID_ERRORMESSAGE_DATE_ERRORMESSAGE"), Array(arrDateTimeCheck(0), strDateTime)
  905.                 WScript.quit(getResource("EXIT_INVALID_INPUT"))
  906.                 Exit Function
  907.         End If
  908.  
  909.         ' split the time to hour, minute and second. Check for bounds
  910.         arrTemp = split(arrDateTimeCheck(1), ":",3,VBBinaryCompare)
  911.  
  912.         intHour   = arrTemp(0)
  913.         intMinute = arrTemp(1)
  914.         intSecond = Left(arrTemp(2), (Len(arrTemp(2))-2)) ' remove the am or pm
  915.  
  916.         If ((CInt(intHour) < 1) OR (CInt(intHour) > 12)     OR _
  917.             (CInt(intMinute) < 0) OR (CInt(intMinute) > 59) OR _
  918.             (CInt(intSecond) < 0) OR (CInt(intSecond) > 59)) Then
  919.                 vbPrintf getResource("L_INVALID_ERRORMESSAGE_TIME_ERRORMESSAGE"), Array(arrDateTimeCheck(1),strDateTime)
  920.                 WScript.Quit(getResource("EXIT_INVALID_INPUT"))
  921.                 Exit Function
  922.        End If
  923.  
  924.        ' check if the given date an time are valid
  925.         If IsDate(arrDateTimeCheck(0)) Then
  926.             strTemp = TimeValue(arrDateTimeCheck(1))
  927.             If Err.Number Then
  928.                 Err.Clear
  929.                 vbPrintf getResource("L_INVALID_ERRORMESSAGE_TIME_ERRORMESSAGE"), Array(arrDateTimeCheck(1),strDateTime)
  930.                 WScript.Quit(getResource("EXIT_INVALID_INPUT"))
  931.                 Exit Function
  932.             Else
  933.                 validateDateTime = TRUE
  934.             End If
  935.         Else
  936.             vbPrintf getResource("L_INVALID_ERRORMESSAGE_DATE_ERRORMESSAGE"), Array(arrDateTimeCheck(0), strDateTime)
  937.             WScript.Quit(getResource("EXIT_INVALID_INPUT"))
  938.             Exit Function
  939.         End If
  940.     End Function
  941.  
  942.     '********************************************************************
  943.     '* Function: changeToWMIDateTime
  944.     '*
  945.     '* Purpose:  To format the given date-time
  946.     '*
  947.     '* Input:    
  948.     '*        [in] strDateTime     the date-time string
  949.     '*        [in] strTimeZone    the TimeZone  of the Queried system
  950.     '*
  951.     '* Output:   Returns the formatted date-time string
  952.     '*
  953.     '********************************************************************
  954.      Function changeToWMIDateTime(ByVal strDateTime,strTimeZone)
  955.         ON ERROR RESUME NEXT
  956.         Err.Clear
  957.  
  958.         Dim arrDateTimeCheck  ' to store the date-time values
  959.         Dim strDate           ' to store temporary date value
  960.         Dim arrDate           ' array to store date values(MMDDYYYY)
  961.         Dim strMonth          ' to store Month value
  962.         Dim strYear           ' to store Year value
  963.         Dim strDay            ' to store Day  value
  964.         Dim strTime           ' to store temporary date value
  965.         Dim arrTime           ' array to store date values(MMDDYYYY)
  966.         Dim i                 ' for looping
  967.     Dim iYearPosition     ' Used to hold the position of year in Date.
  968.  
  969.         ' input strDateTime is like "mm/dd/yy|yyyy,hh:mm:ssAM|PM"
  970.         ' input Timezone is like "'+|-' UUU"
  971.  
  972.         arrDateTimeCheck = split(strDateTime,",")
  973.         ' Finally format the  input like "YYYYMMDDHHMMSS.000000+TIMEZONE"
  974.  
  975.         ' first format the month and day. Append the four digit year
  976.     ' Conver the 2 digit year to 4 digit year.
  977.     ' If there are already 4 digits, then don't worry.
  978.  
  979.     iYearPosition = InstrRev(arrDateTimeCheck(0),"/")
  980.     If Len(arrDateTimeCheck(0)) - iYearPosition = 2 Then
  981.         arrDateTimeCheck(0) = Left(arrDateTimeCheck(0),iYearPosition) & Left(Year(Date),2) & Right(arrDateTimeCheck(0),2)
  982.     End If
  983.  
  984.         'now date is mm/dd/yyyy
  985.         'Spliting the array for month,day,year
  986.         arrDate = split(arrDateTimeCheck(0) , "/" )
  987.  
  988.         ' The date, month  must be of 2 digits
  989.         ' If they are of single digit length < 2, append a "0"
  990.         For i=0 to ubound(arrDate) - 1
  991.             If Len(arrDate(i)) < 2 then
  992.                 arrDate(i) = "0" & arrdate(i)
  993.             End If
  994.         Next
  995.  
  996.         strMonth = arrDate(0)
  997.         strDay   = arrDate(1)
  998.         strYear  = arrDate(2)
  999.  
  1000.         'for 'YYYYMMDD' Pattern
  1001.         strDate = strYear & strMonth & strDay        
  1002.  
  1003.         ' Take the Time for formating 
  1004.         strTime  =  arrDateTimeCheck(1) 
  1005.  
  1006.         'NOW arrDateTimeCheck(1)="HH:MM:SSAM|PM".
  1007.         'here formating Time 24Hours independent of Locale separator  
  1008.  
  1009.         'Spliting the array for HH MM SS
  1010.         arrTime = split(strTime , ":" )           
  1011.  
  1012.         'Looking for [A|P]M string 
  1013.        If  Instr(1,Lcase(arrTime(2)),Lcase("AM"),VBBinaryCompare) > 0 Then 
  1014.                    'AM Conversion  for 24H
  1015.                If  arrTime(0) >=  12 Then
  1016.                     arrTime(0) = arrTime(0) - 12 
  1017.                End If        
  1018.  
  1019.         Else
  1020.                     'PM Conversion for 24H
  1021.                If  arrTime(0)  < 12 Then
  1022.                        arrTime(0) =arrTime(0) + 12 
  1023.                End If
  1024.  
  1025.        End If 
  1026.  
  1027.         'Adding leading zero  if third element  is  S[A|P]M
  1028.         If Len( arrTime(2)) = 3 then   arrTime(2)  = "0" & arrTime(2)  
  1029.  
  1030.         'Removing  AM|PM from  third  element in the  array 
  1031.         arrTime(2) =Mid(arrTime(2),1,2) 
  1032.  
  1033.         ' The hours, mins and secs must be of 2 digits
  1034.         ' If they are of single digit i.e Len < 2 , append a "0"
  1035.         For i=0 to ubound(arrTime) 
  1036.                 If Len(arrTime(i)) < 2 then
  1037.                        arrTime(i) = "0" & arrTime(i)
  1038.                 End If
  1039.         Next
  1040.  
  1041.         strTime = Join( arrTime ,"") ' formatting as HHMMSS
  1042.  
  1043.         ' Return the total format as "YYYYMMDDHHMMSS.000000+TIMEZONE"
  1044.          ChangeToWMIDateTime = strDate & strTime & ".000000" & strTimeZone
  1045.  
  1046. End Function
  1047.  
  1048.     ]]>
  1049. </script>
  1050. </component>
  1051. </package>
  1052.